package com.smart.base.tools.generator.pojo;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

import com.smart.base.tools.generator.pojo.consts.GenerateEnum;
import com.smart.base.tools.generator.pojo.db.AnalysisDB;
import com.smart.base.tools.generator.pojo.model.TableMeta;
import com.smart.base.utils.BaseUtil;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * @ClassName：GenerateMain @Description：
 * @author：shisongbin
 * @date：2016年5月24日 下午4:45:58
 */
public class GenerateMain {

	public static final String AUTHOR_BY = "wangshaokui@ssit-xm.com.cn";

	/** 生成表 */
	public static final String[] GENERATE_TABLES = new String[] { "sys_star"};

	/** 所要生成的类 */
	public static String[] GENERATE_CLASS_ARRAY = { GenerateEnum.ENTITY.getName(), GenerateEnum.ORM.getName(), GenerateEnum.DAO.getName(),
			GenerateEnum.DTO.getName(),GenerateEnum.SERVICE.getName(),GenerateEnum.SERVICEIMPL.getName(),GenerateEnum.CONTROLLER.getName() };

	/** 需要剔除 表前缀 */
	public static final String TABLE_PREFIX = "sys_";

	/** 代码生成包路径 */
	public static final String PACK_NAME = BaseUtil.isEmpty(TABLE_PREFIX)?  "" : TABLE_PREFIX.replace("_", "");

	/** 代码生成项目路径 */
	public static final String CODE_PROJECT_PATH = "d:/temp/code";

	public static void main(String[] args) throws IOException, TemplateException {
		List<TableMeta> tableList;
		Writer out = null;

		tableList = AnalysisDB.readDB(GENERATE_TABLES);
		AnalysisDB.readTables(tableList);

		if (GENERATE_CLASS_ARRAY != null && GENERATE_CLASS_ARRAY.length > 0) {
			for (String cla : GENERATE_CLASS_ARRAY) {

				String targetDir = getClassTargetDir(cla);
				// 输出到文件
				File dir = new File(targetDir);
				if (!dir.isDirectory()) {
					dir.mkdirs();
				}

				Configuration cfg = new Configuration();
				cfg.setClassForTemplateLoading(GenerateMain.class, "/generator/pojo");
				cfg.setObjectWrapper(new DefaultObjectWrapper());
				Template tpl = cfg.getTemplate(cla.replace("/", "") + ".ftl");

				if (tableList != null) {
					for (int i = 0; i < tableList.size(); i++) {
						TableMeta tm = tableList.get(i);
						String packageDir = PACK_NAME;
						tm.setPackageDir(packageDir);
						tm.setAuthor(AUTHOR_BY);
						if (BaseUtil.isEmpty(tm.getClassName())) {
							continue;
						}

						if (GenerateEnum.ORM.getName().equals(cla)) {
							out = new FileWriter(new File(targetDir + getClassName(tm.getClassName(), cla) + ".xml"));
						} else {
							out = new FileWriter(new File(targetDir + getClassName(tm.getClassName(), cla) + ".java"));
						}

						tpl.process(tm, out);
						System.out.println("===文件 " + getClassName(tm.getClassName(), cla) + ".java" + " 生成成功===");
					}
				}
			}

		}
		out.flush();
		out.close();

	}

	private static String getClassTargetDir(String cla) {
		if (GenerateEnum.ENTITY.getName().equals(cla)) {
			return CODE_PROJECT_PATH + "/domain/";
		}
		if (GenerateEnum.DTO.getName().equals(cla)) {
			return CODE_PROJECT_PATH + "/domain/";
		}
		if (GenerateEnum.ORM.getName().equals(cla)) {
			return CODE_PROJECT_PATH + "/dao/";
		}
		if (GenerateEnum.DAO.getName().equals(cla)) {
			return CODE_PROJECT_PATH + "/dao/";
		}
		if (GenerateEnum.CONTROLLER.getName().equals(cla)) {
			return CODE_PROJECT_PATH + "/controller/";
		}
		if (GenerateEnum.SERVICE.getName().equals(cla)) {
			return CODE_PROJECT_PATH + "/service/";
		}
		if (GenerateEnum.SERVICEIMPL.getName().equals(cla)) {
			return CODE_PROJECT_PATH + "/serviceImpl/";
		}
		return null;
	}

	private static String getClassName(String className, String cla) {
		if (GenerateEnum.ENTITY.getName().equals(cla)) {
			return className;
		}
		if (GenerateEnum.ORM.getName().equals(cla)) {
			return className + "Dao";
		}
		if (GenerateEnum.DAO.getName().equals(cla)) {
			return className + "Dao";
		}
		if (GenerateEnum.DTO.getName().equals(cla)) {
			return className + "Dto";
		}
		if (GenerateEnum.CONTROLLER.getName().equals(cla)) {
			return className + "Controller";
		}
		if (GenerateEnum.SERVICE.getName().equals(cla)) {
			return "I"+className + "Service";
		}
		if (GenerateEnum.SERVICEIMPL.getName().equals(cla)) {
			return className + "ServiceImpl";
		}
		return className + "Other";
	}
}
